<?php
class newsMod extends commonMod
{
	public function __construct()
	{
		parent::__construct();

	}
	//信息列表页面
	public function index()
	{	
		$listRows=20;//每页显示的信息条数
		$cur_page=isset($_GET['page'])?intval($_GET['page']):1;//获取当前分页
		$cur_page=$cur_page<1?1:$cur_page;//当前页小于1，则设当前页为1
		$limit_start=($cur_page-1)*$listRows;
		$limit=$limit_start.','.$listRows;
		$where="";
		$condition=array();
		$cat_id=intval($_GET[0]);
		
		$url=__URL__.'/index.html';//分页基准网址
		
		//某分类下的文章
		if(!empty($cat_id))
		{			
			$condition['cid']=$cat_id;
			$cur_cat=$this->model->table('category')->where($condition)->find();				
			$this->assign('cur_cat',$cur_cat);//当前分类
			unset($condition);
			$where='WHERE A.cid='.$cat_id;
			$condition['cid']=$cat_id;
			$url=__URL__.'/index-'.$cat_id.'.html';//分页基准网址
		}
		//获取行数
		$count=$this->model->table('content')->where($condition)->count();	
		
		//联合查询,通过分类id，查询出分类名称
		$sql="SELECT * FROM {$this->model->pre}content LEFT JOIN {$this->model->pre}category ON {$this->model->pre}content.cid = {$this->model->pre}category.cid {$where} ORDER BY {$this->model->pre}content.id DESC LIMIT {$limit}";
		//$this->model->pre,数据表前缀
		
		$list=$this->model->query($sql);//执行查询
		
		$this->assign('list',$list);
		$this->assign('page',$this->page($url,$count,$listRows));
		$this->display();
	}
	
	//发布资讯
	public function add()
	{	
		if(empty($_POST['do']))
		{
			$cat=module('category')->getCat();//获取格式化后的分类树
			$info['cid']=intval($_GET[0]);//文章分类
			
			$this->assign('info',$info);
			$this->assign('cat',$cat);			
			$this->display();
			return;
		}
		
		//获取数据
		$data=array();
		$data['title']=in($_POST['title']);//文章标题
		$data['cid']=intval($_POST['cid']);//文章分类
		$data['tags']=in($_POST['tags']);//关键词
		$data['content']=html_in($_POST['content']);//内容
		$data['ontop']=in($_POST['ontop']);//推荐
		$data['status']=in($_POST['status']);//状态
		$data['addtime']=time();
		$data['author']=$_SESSION['nicename'];
		$data['pic']=$_POST['pic'];   
		
		//验证数据
		if(empty($data['title']))
		{
			$this->error('标题不能为空');
		}
		if(empty($data['cid']))
		{
			$this->error('请选择文章分类');
		}
		if(empty($data['content']))
		{
			$this->error('内容不能为空');
		}
		
		//数据库操作		
		if($this->model->table('content')->data($data)->insert())
		
			$this->success('资讯发布成功!');
		else
			$this->error('资讯发布失败!');
	}
	
	public function edit()
	{
		//显示资讯编辑页面
		if(empty($_POST['do']))
		{
			$id=intval($_GET[0]);
			if(empty($id))
			{
				$this->error('参数传递错误!');
			}
			$condition['id']=$id;
			$info=$this->model->table('content')->where($condition)->find();//获取当前文章信息
			if(empty($info))
			{
				$this->error('该分类不存在或者已被删除!');
			}
			
			//调用category分类模块的getCat方法
			$cat=module('category')->getCat();//获取格式化后的分类树
			
			$this->assign('info',$info);
			$this->assign('category',$cat);	
			$this->display();
			return;
		}
		//获取数据
		$data=array();
		$condition['id']=$_POST['id'];
		$data['title']=in($_POST['title']);//文章标题
		$data['cid']=intval($_POST['cid']);//文章分类
		$data['tags']=in($_POST['tags']);//关键词
		$data['dec']=html_in($_POST['dec']);//描述
		$data['content']=html_in($_POST['content']);//内容
		$data['ontop']=in($_POST['ontop']);//推荐
		$data['status']=in($_POST['status']);//状态
		$data['pic']=in($_POST['pic']);//缩略图
		$data['addtime']=time();
		$data['author']=$_SESSION['nicename'];
		
		//验证数据
		if(empty($data['title']))
		{
			$this->error('标题不能为空');
		}
		if(empty($data['cid']))
		{
			$this->error('请选择文章分类');
		}
		if(empty($data['content']))
		{
			$this->error('内容不能为空');
		}
		//数据库操作
		if($this->model->table('content')->data($data)->where($condition)->update()){
			
			$this->success('信息更新成功!');
		}
		else{
			$this->error('信息更新失败!');
		}
	}
	//删除资讯
	public function del()
	{
		if(empty($_POST)){
			$id=intval($_GET[0]);
			if(empty($id))
			{
				$this->error('参数传递错误');
			}
			$condition['id']=$id;
			if($this->model->table('content')->where($condition)->delete())
				$this->success('删除成功!');
			else
				$this->error('删除失败!');
		}else{
			if(empty($_POST['delid'])){
					$this->error('你没有选择任何参数！');
				}	
				$delid=implode(',',$_POST['delid']);
				$list=$this->model->table('content')->where('id in ('.$delid.')')->delete();
				
				$this->success('删除信息成功！');
		}
	}
	
	
	
}
?>